import datetime
from typing import Optional

from pydantic import BaseModel
from sqlalchemy import BigInteger, Index, Integer, PrimaryKeyConstraint, String
from sqlalchemy.orm import Mapped, mapped_column
from sqlalchemy.dialects.postgresql import TIMESTAMP

from commons.PaginationBase import PaginationBase
from datebase import Base


class AmSysUser(Base):
    __tablename__ = 'am_sys_user'
    __table_args__ = (
        PrimaryKeyConstraint('id', name='am_sys_user_pkey'),
        Index('idx_user_phone', 'phone'),
        Index('idx_user_username', 'username'),
        {'comment': '后台用户表', 'schema': 'test_one'}
    )

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    username: Mapped[Optional[str]] = mapped_column(String(64), comment='用户名(登录用)')
    password: Mapped[Optional[str]] = mapped_column(String(64), comment='密码')
    icon: Mapped[Optional[str]] = mapped_column(String(500), comment='头像')
    email: Mapped[Optional[str]] = mapped_column(String(100), comment='邮箱')
    nick_name: Mapped[Optional[str]] = mapped_column(String(200), comment='昵称')
    note: Mapped[Optional[str]] = mapped_column(String(500), comment='备注信息')
    create_time: Mapped[Optional[datetime.datetime]] = mapped_column(TIMESTAMP(precision=6), comment='创建时间')
    login_time: Mapped[Optional[datetime.datetime]] = mapped_column(TIMESTAMP(precision=6), comment='最后登录时间')
    status: Mapped[Optional[int]] = mapped_column(Integer, comment='帐号启用状态：0->禁用；1->启用')
    phone: Mapped[Optional[str]] = mapped_column(String(20), comment='手机号(登录用)')
    create_id: Mapped[Optional[int]] = mapped_column(BigInteger, comment='创建用户')
    update_time: Mapped[Optional[datetime.datetime]] = mapped_column(TIMESTAMP(precision=6), comment='更新时间')
    update_id: Mapped[Optional[int]] = mapped_column(BigInteger, comment='更新用户')
    fsite_no_belong: Mapped[Optional[str]] = mapped_column(String(20), comment='所属地区')
    ftype: Mapped[Optional[int]] = mapped_column(Integer, comment='用户类型(2前端，1后端，3都是)')
    fpoint_noes: Mapped[Optional[str]] = mapped_column(String(255), comment='教学楼编号')
    fcard: Mapped[Optional[str]] = mapped_column(String(20), comment='用户卡号')
    password_clear: Mapped[Optional[str]] = mapped_column(String(64), comment='明文密码')
    fappno: Mapped[Optional[str]] = mapped_column(String(64), comment='平板编号')
    enterprise_wechat: Mapped[Optional[str]] = mapped_column(String(100), comment='企业微信')


class AmSysUserCreate(BaseModel):
    id: Optional[int] = None
    username: Optional[str] = None
    password: Optional[str] = None
    icon: Optional[str] = None
    email: Optional[str] = None
    nickName: Optional[str] = None
    note: Optional[str] = None
    createTime: Optional[datetime.datetime] = None
    loginTime: Optional[datetime.datetime] = None
    status: Optional[int] = None
    phone: Optional[str] = None
    createId: Optional[int] = None
    updateTime: Optional[datetime.datetime] = None
    updateId: Optional[int] = None
    fsiteNoBelong: Optional[str] = None
    ftype: Optional[int] = None
    fpointNoes: Optional[str] = None
    fcard: Optional[str] = None
    passwordClear: Optional[str] = None
    fappno: Optional[str] = None
    enterpriseWechat: Optional[str] = None


class AmSysUserQO(PaginationBase):
    username: Optional[str] = None
    email: Optional[str] = None
